package com.kai.steering.data.service.entity.domainObject;

import cn.hutool.core.bean.BeanUtil;
import com.kai.steering.common.vo.Result;
import com.kai.steering.data.service.entity.dto.NoteQueryDTO;
import com.kai.steering.data.service.entity.dto.QuestionDTO;
import com.kai.steering.data.service.entity.dto.QuestionQueryDTO;
import com.kai.steering.data.service.entity.po.Answer;
import com.kai.steering.data.service.entity.po.Classification;
import com.kai.steering.data.service.entity.po.Question;
import com.kai.steering.data.service.entity.po.QuestionDetails;
import com.kai.steering.data.service.service.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * 题目 Do对象
 * </pre>
 *
 * @author kai
 * @date 2022-02-23
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "QuestionDO对象", description = "题目业务实体")
public class QuestionDO implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "题目id")
    private String questionId;
    @ApiModelProperty(value = "题目")
    private String question;
    @ApiModelProperty(value = "题目详情")
    private QuestionDetailsDO questionDetailsDO;
    @ApiModelProperty(value = "答案详情")
    private List<AnswerDO> answerDO;
    @ApiModelProperty(value = "创建人管理员id或者用户id")
    private String founder;
    @ApiModelProperty(value = "创建时间")
    private Date creationDatetime;
    @ApiModelProperty(value = "题目类型分类")
    private ClassificationDO classificationDO;
    @ApiModelProperty(value = "知识点 以,分割")
    private List<KnowledgeDO> knowledgeDOs;
    @ApiModelProperty(value = "存在0删除1")
    private Integer usable;
    @ApiModelProperty(value = "删除日期")
    private Date deleteDatetime;
    @ApiModelProperty(value = "用户与题目")
    private UserQuestionDO userQuestionDO;
    @ApiModelProperty(value = "标准答案")
    private AnswerDO standardAnswer;

    public QuestionDO(QuestionQueryDTO questionQueryDTO) {
        this.convertQuestionDO(questionQueryDTO);
    }
    public QuestionDO(QuestionDTO questionDTO) {
        this.convertQuestionDO(questionDTO);
    }


    public QuestionDO(Question question) {
        this.convertQuestionDO(question);

    }

    public QuestionDO(String questionId) {
        this.questionId=questionId;
    }

    public QuestionDO convertQuestionDO(QuestionDTO questionDTO) {
        this.questionId=questionDTO.getQuestionId();
        this.question=questionDTO.getQuestion();
        this.founder=questionDTO.getFounder();
        this.creationDatetime=questionDTO.getCreationDatetime();
        this.classificationDO=new ClassificationDO(questionDTO.getClassificationId());
        this.deleteDatetime=questionDTO.getDeleteDatetime();

        if (questionDTO.getQuestionDetails()!=null){
            this.setQuestionDetailsDO(new QuestionDetailsDO(questionDTO.getQuestionDetails()));
        }
        if (questionDTO.getAnswerDTO()!=null){
            List<AnswerDO> collect = questionDTO.getAnswerDTO().stream().map(answerDTO -> {
                return new AnswerDO(answerDTO);
            }).collect(Collectors.toList());
            this.setAnswerDO(collect);
        }
        if (questionDTO.getKnowledges()!=null) {
            this.knowledgeDOs = Arrays.stream(questionDTO.getKnowledges()).map
                    ( knowledgeS-> {
                        return new KnowledgeDO(knowledgeS);
                    }).collect(Collectors.toList());
        }



        return this;
    }


    public QuestionDO convertQuestionDO(Question question) {
        this.questionId=question.getQuestionId();
        this.question=question.getQuestion();
        this.founder=question.getFounder();
        this.creationDatetime=question.getCreationDatetime();
        this.classificationDO=new ClassificationDO(question.getClassificationId());
        this.deleteDatetime=question.getDeleteDatetime();
        if (question.getKnowledgeId()!=null) {
            this.knowledgeDOs= KnowledgeDO.converKnowledgeDO(question.getKnowledgeId());
        }
        return this;
    }



    public QuestionDO convertQuestionDO(QuestionQueryDTO questionQueryDTO){
        this.questionId=questionQueryDTO.getQuestionId();
        this.question=questionQueryDTO.getQuestion();
//        this.answerDO(questionId);



        this.founder=null;
        this.creationDatetime=null;
        this.classificationDO=new ClassificationDO(questionQueryDTO.getClassificationId());

        this.deleteDatetime=null;
        return this;
    }
}